// 监听插件安装或更新事件
chrome.runtime.onInstalled.addListener((details) => {
  if (details.reason === "install") {
    // 插件首次安装
    console.log("B站视频倍速增强插件已安装");

    // 初始化默认设置
    chrome.storage.sync.set({
      customSpeeds: [1.35, 1.45, 1.75],
      lastUsedSpeed: null,
      enableKeyboardShortcuts: true,
    });
  } else if (details.reason === "update") {
    // 插件更新
    console.log(
      `B站视频倍速增强插件已更新到版本 ${chrome.runtime.getManifest().version}`
    );
  }
});

// 监听消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === "getSettings") {
    // 获取设置
    chrome.storage.sync.get(
      {
        customSpeeds: [1.35, 1.45, 1.75],
        lastUsedSpeed: null,
        enableKeyboardShortcuts: true,
      },
      (items) => {
        sendResponse(items);
      }
    );
    return true; // 异步响应
  } else if (message.action === "saveSettings") {
    // 保存设置
    chrome.storage.sync.set(message.settings, () => {
      sendResponse({ success: true });
    });
    return true; // 异步响应
  }
});
